package transport

import (
	"context"
	"dssh/models"
	"encoding/json"
	"fmt"
	"gitee.com/dingyangzhuang/dingutils/dlog/logger"
	"net/http"
)

// todo 这一步的作用就是接受参数，然后传递给定义的Request对象，交给Endpoint去处理
func DecodeCustomRequest(ctx context.Context, r *http.Request) (interface{}, error) {
	var data = models.ModelCustomRequest{}
	err := json.NewDecoder(r.Body).Decode(&data)
	if err != nil {
		logger.Error("参数传递错误", "error", err.Error(), "body", r.GetBody)
		return nil, err
	}
	fmt.Printf("%+v\n", data)
	logger.Info("收到指令IP", "ip", data.Ip, "指令", data.Instruct, "指令参数", data.Args)
	//decoder := json.NewDecoder(r.Body)
	//fmt.Printf("yyyy%+v\n", decoder)
	//todo 相当于从http://127.0.0.1?uid=101 这样获取uid参数
	return data, nil
}

func EncodeCunstomResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error {
	w.Header().Set("Content-type", "application/json")
	return json.NewEncoder(w).Encode(response)
}
